package com.css.zfzx.sjcj.modules.classifygroup.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.classifygroup.repository.ZfzxClassifyGroupNativeRepository;
import com.css.zfzx.sjcj.modules.classifygroup.repository.ZfzxClassifyGroupRepository;
import com.css.zfzx.sjcj.modules.classifygroup.repository.entity.ZfzxClassifyGroupEntity;
import com.css.zfzx.sjcj.modules.classifygroup.service.ZfzxClassifyGroupService;
import com.css.zfzx.sjcj.modules.classifygroup.viewobjects.ZfzxClassifyGroupQueryParams;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tao
 * @date 2020-11-06
 */
@Service
public class ZfzxClassifyGroupServiceImpl implements ZfzxClassifyGroupService {

	@Autowired
    private ZfzxClassifyGroupRepository zfzxClassifyGroupRepository;
    @Autowired
    private ZfzxClassifyGroupNativeRepository zfzxClassifyGroupNativeRepository;

    @Override
    public JSONObject queryZfzxClassifyGroups(ZfzxClassifyGroupQueryParams queryParams, int curPage, int pageSize) {
        Page<ZfzxClassifyGroupEntity> zfzxClassifyGroupPage = this.zfzxClassifyGroupNativeRepository.queryZfzxClassifyGroups(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(zfzxClassifyGroupPage);
        return jsonObject;
    }


    @Override
    public ZfzxClassifyGroupEntity getZfzxClassifyGroup(String id) {
        ZfzxClassifyGroupEntity zfzxClassifyGroup = this.zfzxClassifyGroupRepository.findById(id).orElse(null);
         return zfzxClassifyGroup;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZfzxClassifyGroupEntity saveZfzxClassifyGroup(ZfzxClassifyGroupEntity zfzxClassifyGroup) {
        String uuid = UUIDGenerator.getUUID();
        zfzxClassifyGroup.setId(uuid);
        zfzxClassifyGroup.setCreateUser(PlatformSessionUtils.getUserId());
        zfzxClassifyGroup.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.zfzxClassifyGroupRepository.save(zfzxClassifyGroup);
        return zfzxClassifyGroup;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZfzxClassifyGroupEntity updateZfzxClassifyGroup(ZfzxClassifyGroupEntity zfzxClassifyGroup) {
        zfzxClassifyGroup.setUpdateUser(PlatformSessionUtils.getUserId());
        zfzxClassifyGroup.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.zfzxClassifyGroupRepository.save(zfzxClassifyGroup);
        return zfzxClassifyGroup;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteZfzxClassifyGroups(List<String> ids) {
        List<ZfzxClassifyGroupEntity> zfzxClassifyGroupList = this.zfzxClassifyGroupRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(zfzxClassifyGroupList) && zfzxClassifyGroupList.size() > 0) {
            for(ZfzxClassifyGroupEntity zfzxClassifyGroup : zfzxClassifyGroupList) {
                this.zfzxClassifyGroupRepository.delete(zfzxClassifyGroup);
            }
        }
    }


}
